﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WjAutoConfigApp.Common;
using WjAutoConfigApp.Models;

namespace WjAutoConfigApp
{
    public partial class LaneAllPropertiesSetingForm : Form
    {
        private bool isEdit;
        public LaneAllPropertiesSetingForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            WaitWindowRotate.Show();
            //1.校验
            if (!CheckData()) return;
            //2.将界面配置转换为模型
            LaneAllPropertyModel model = new LaneAllPropertyModel();
            model.LaneNum = laneNoText.Text;
            model.With = laneWithText.Text;
            model.Long = laneLongText.Text;
            model.StartX = startXText.Text;
            model.StartY= startYText.Text;
            model.EndX = endXText.Text;
            model.EndY = endYText.Text;
            model.Direction = combDirection.SelectedValue.ToString();
            model.IsUnMotor = combUnMotor.SelectedValue.ToString();
            model.IsSpecial = combSpecial.SelectedValue.ToString();
            model.IsMeet = combmeet.SelectedValue.ToString();
            model.Speed = speedText.Text;


            //3.修改对应车道信息CSV矩阵下的数值
            var value = model.ToString();
            GlobleCache.laneInfoCsv.AddRow(value.Split(';'));

            //4.将车道编号写入到对应的车道属性CSV中
            if (isEdit)
            {
                CSVHelper.EditLaneInfo();
            }
            //4.1.获取所有符合条件的像素点
            var matchPoints = CommomMethod.GetMatchPoints(GlobleCache.areaModel.Points);
            matchPoints.ForEach(p => CSVHelper.SetCsvPointValue(p, GlobleCache.lanePropertyCsv,0,model.LaneNum));
            GlobleCache.areaModel.IsLaneArea = true;
            GlobleCache.areaModel.LaneInfoModel = model;
            GlobleCache.areaModel.IsSaved = true;
            CommomMethod.SaveFinishArea(GlobleCache.areaModel);
            GlobleCache.areaModel.InitAreaModel();
            WaitWindowRotate.Hide();
            this.Close();
        }

        public static void SetCsvPointValue(Point p,CSV csv,int index,string value)
        {
            var dataValue = csv.GetData(p.Y, p.X);
            var dataArr = dataValue.Split(ConstantValue.PROPERTY_DATA_SPLIT);
            dataArr[index] = value;
            var content = string.Join(ConstantValue.PROPERTY_DATA_SPLIT.ToString(), dataArr);
            csv.SetData(p.Y, p.X, content);
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 加载完成事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LanePropertiesSetingForm_Load(object sender, EventArgs e)
        {
            InitData();
            if (GlobleCache.areaModel.LaneInfoModel is null) return;

            isEdit = true;
            //将面板内容反显到面板上
            laneNoText.Text = GlobleCache.areaModel.LaneInfoModel.LaneNum;
            laneWithText.Text = GlobleCache.areaModel.LaneInfoModel.With;
            laneLongText.Text = GlobleCache.areaModel.LaneInfoModel.Long;
            startXText.Text = GlobleCache.areaModel.LaneInfoModel.StartX;
            endXText.Text = GlobleCache.areaModel.LaneInfoModel.EndX;
            startYText.Text = GlobleCache.areaModel.LaneInfoModel.StartY;
            endYText.Text = GlobleCache.areaModel.LaneInfoModel.EndY;
            speedText.Text = GlobleCache.areaModel.LaneInfoModel.Speed;
            combDirection.SelectedIndex = int.Parse(GlobleCache.areaModel.LaneInfoModel.Direction);
            combmeet.SelectedIndex = int.Parse(GlobleCache.areaModel.LaneInfoModel.IsMeet);
            combSpecial.SelectedIndex = int.Parse(GlobleCache.areaModel.LaneInfoModel.IsSpecial);
            combUnMotor.SelectedIndex = int.Parse(GlobleCache.areaModel.LaneInfoModel.IsUnMotor);
        }


        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            
            List<CommonContent> commonData = new List<CommonContent>();
            commonData.Add(new CommonContent { Code = 0, Value = "否" });
            commonData.Add(new CommonContent { Code = 1, Value = "是" });
            List<CommonContent> commonData1 = new List<CommonContent>();
            commonData1.Add(new CommonContent { Code = 0, Value = "否" });
            commonData1.Add(new CommonContent { Code = 1, Value = "是" });
            List<CommonContent> commonData2 = new List<CommonContent>();
            commonData2.Add(new CommonContent { Code = 0, Value = "否" });
            commonData2.Add(new CommonContent { Code = 1, Value = "是" });
            //是否非机动车道/专用车道/应急车道
            combUnMotor.DataSource = commonData;
            combUnMotor.DisplayMember = nameof(CommonContent.Value);
            combUnMotor.ValueMember = nameof(CommonContent.Code);

            combSpecial.DataSource = commonData1;
            combSpecial.DisplayMember = nameof(CommonContent.Value);
            combSpecial.ValueMember = nameof(CommonContent.Code);

            combmeet.DataSource = commonData2;
            combmeet.DisplayMember = nameof(CommonContent.Value);
            combmeet.ValueMember = nameof(CommonContent.Code);
            //车道方向
            List<CommonContent> dirctionData = new List<CommonContent>();
            dirctionData.Add(new CommonContent { Code = 0, Value = "直行" });
            dirctionData.Add(new CommonContent { Code = 1, Value = "左转" });
            dirctionData.Add(new CommonContent { Code = 2, Value = "右转" });
            dirctionData.Add(new CommonContent { Code = 3, Value = "直左" });
            dirctionData.Add(new CommonContent { Code = 4, Value = "直右" });
            dirctionData.Add(new CommonContent { Code = 5, Value = "直左右" });
            dirctionData.Add(new CommonContent { Code = 6, Value = "未知" });
            combDirection.DataSource = dirctionData;
            combDirection.DisplayMember = nameof(CommonContent.Value);
            combDirection.ValueMember = nameof(CommonContent.Code);
        }

        /// <summary>
        /// 校验表单填写的正确性
        /// </summary>
        private bool CheckData()
        {
            if (!CommomMethod.IsIntData(laneNoText.Text))
            {
                CommomMethod.ShowMsg("车道号只能是正整数");
                return false;
            }
            if (!CommomMethod.IsDoubleData(laneWithText.Text))
            {
                CommomMethod.ShowMsg("车道宽度不符合格式要求");
                return false;
            }
            if (!CommomMethod.IsDoubleData(laneLongText.Text))
            {
                CommomMethod.ShowMsg("车道长度不符合格式要求");
                return false;
            }
            if (!CommomMethod.IsIntData(startXText.Text)||
                !CommomMethod.IsIntData(startYText.Text) ||
                !CommomMethod.IsIntData(endXText.Text) ||
                !CommomMethod.IsIntData(endYText.Text) 
                )
            {
                CommomMethod.ShowMsg("车道起始点坐标不符合格式要求");
                return false;
            }

            if (!CommomMethod.IsFloatData(speedText.Text))
            {
                CommomMethod.ShowMsg("速度值不符合格式要求");
                return false;
            }
            return true;
        }


    }
}
